package org.huawei.dao;

import org.huawei.model.domain.Submission;
import org.huawei.utils.DatabaseUtil;
import org.huawei.view.AlartView;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SubmissionDao {
    public List<Submission> selectSubmissionByHomeworkId(int homeworkId) throws SQLException {
        List<Submission> submissions = new ArrayList<>();

        // 定义查询 SQL
        String query = "SELECT * FROM Submission WHERE HomeworkId = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(query)) {

            // 设置查询参数（HomeworkId）
            stmt.setInt(1, homeworkId);

            // 执行查询操作
            try (ResultSet rs = stmt.executeQuery()) {
                // 遍历结果集
                while (rs.next()) {
                    // 创建 Submission 对象并设置各字段
                    Submission submission = new Submission();
                    submission.setSubmissionId(rs.getInt("SubmissionId"));
                    submission.setHomeworkId(rs.getInt("HomeworkId"));
                    submission.setSubmittedBy(rs.getInt("SubmittedBy"));
                    submission.setSubmissionContent(rs.getString("SubmissionContent"));
                    submission.setSubmissionFeedback(rs.getString("SubmissionFeedback"));
                    submission.setSubmissionGrade(rs.getDouble("SubmissionGrade"));
                    submission.setSubmissionCreationDate(rs.getDate("SubmissionCreationDate"));

                    // 将每个 Submission 对象添加到列表
                    submissions.add(submission);
                }
            }
        }

        return submissions;  // 返回提交列表
    }


    public void updateSubmissionInDatabase(Submission submission) throws SQLException {
        String query = "UPDATE submission SET HomeworkId = ?, SubmittedBy = ?, SubmissionContent = ?, SubmissionFeedback = ?, SubmissionGrade = ?, SubmissionCreationDate = ? WHERE SubmissionId = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(query)) {

            stmt.setInt(1, submission.getHomeworkId());
            stmt.setInt(2, submission.getSubmittedBy());
            stmt.setString(3, submission.getSubmissionContent());
            stmt.setString(4, submission.getSubmissionFeedback());
            stmt.setDouble(5, submission.getSubmissionGrade());

            // 将 java.util.Date 转换为 java.sql.Date
            java.util.Date utilDate = submission.getSubmissionCreationDate();
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            stmt.setDate(6, sqlDate);

            // 设置第7个参数 SubmissionId
            stmt.setInt(7, submission.getSubmissionId());

            stmt.executeUpdate();
        } catch (SQLException e) {
            AlartView.alart(null, "更新失败");
            e.printStackTrace();
        }
    }

    public void submitHomework(Submission submission) throws SQLException {
        String query = "INSERT INTO submission (homeworkId, submittedBy, submissionContent, submissionFeedback, submissionGrade, submissionCreationDate) "
                + "VALUES (?, ?, ?, ?, ?, ?)";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(query)) {
            pstmt.setInt(1, submission.getHomeworkId());
            pstmt.setInt(2, submission.getSubmittedBy());
            pstmt.setString(3, submission.getSubmissionContent());
            pstmt.setString(4, submission.getSubmissionFeedback());
            pstmt.setDouble(5, submission.getSubmissionGrade());
            pstmt.setDate(6, new java.sql.Date(submission.getSubmissionCreationDate().getTime()));

            pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("Error while submitting homework: " + e.getMessage(), e);
        }
    }
}
